package com.songcha.module_main.ui.view


import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.FontMetrics
import android.graphics.Path
import android.graphics.RectF
import com.songcha.library_calendar.calendarview.Calendar
import com.songcha.library_calendar.calendarview.LunarCalendarUtil
import com.songcha.library_calendar.calendarview.LunarConvertUtil
import com.songcha.library_calendar.calendarview.MonthView
import com.songcha.library_common.util.FontUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px
import com.songcha.library_picker.pickerview.utils.LunarDateUtil
import java.lang.Integer.min
import com.songcha.module_main.R

class MyMonthView(context: Context): MonthView(context) {
    private lateinit var mTextPaint:Paint
    private lateinit var mSelectBgPaint:Paint
    private lateinit var mCurrentDayBgPaint:Paint

    private val mTextColor = Color.parseColor("#FFE1CC")
    private val mWeekendTextColor = Color.parseColor("#CE3434")
    private val mOtherMonthTextColor = Color.parseColor("#D9D9D9")
    private val mSelectBgColor = Color.parseColor("#DC5252")
    private val mSelectTextColor = Color.parseColor("#1A1A1A")
    private val mSelectBgRadius = 11f.dp2px()
    private val mTodayBgRadius = 11f.dp2px()
    private val mCurrentDayTextColor = Color.parseColor("#A86436")
    private val mCurrentDayStrokeWidth = 1f.dp2px()
    private val mTextSize = 13f.sp2px()
    private val mCurDayClipPath = Path()
    //private var mJiriDayClipPath = Path()
    private val mItemGap = 1.5f.dp2px()
    private lateinit var mLeafBitmap:Bitmap
    private val mLeafWidth = 50.dp2px()
    private val mLeafHeight = 38.dp2px()


    companion object{

    }

    init {

        //val fontScale =  ScreenUtil.getFontScale()

        mTextPaint = Paint()
        mTextPaint.isAntiAlias = true
        mTextPaint.textSize = mTextSize
        mTextPaint.color = mTextColor
        mTextPaint.isFakeBoldText = true

        mSelectBgPaint = Paint()
        mSelectBgPaint.isAntiAlias = true
        mSelectBgPaint.color = mSelectBgColor

        mCurrentDayBgPaint = Paint()
        mCurrentDayBgPaint.isAntiAlias = true
        mCurrentDayBgPaint.style = Paint.Style.STROKE
        mCurrentDayBgPaint.strokeWidth = mCurrentDayStrokeWidth
        mCurrentDayBgPaint.color = mCurrentDayTextColor

        mLeafBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources,R.mipmap.main_icon_calendar_leaf),mLeafWidth,mLeafHeight,false)

    }

    private fun drawRoundRect(canvas: Canvas,x:Int,y:Int,radius:Float,paint:Paint){
        val rect:RectF

        if(mItemWidth > mItemHeight){
            val offset = (mItemWidth-mItemHeight) / 2
            rect=RectF((x + offset).toFloat(),(y + mItemGap).toFloat(),(x + mItemWidth-offset).toFloat(),(y + mItemHeight - mItemGap).toFloat())
        }else if(mItemWidth < mItemHeight){
            val offset=(mItemHeight - mItemWidth) / 2
            rect=RectF((x + mItemGap).toFloat(),(y + offset).toFloat(),(x + mItemWidth-mItemGap).toFloat(),(y + mItemHeight - offset).toFloat())
        }else{
            rect=RectF((x + mItemGap).toFloat(),(y + mItemGap).toFloat(),(x + mItemWidth-mItemGap).toFloat(),(y + mItemHeight - mItemGap).toFloat())
        }
        canvas.drawRoundRect(rect,radius,radius,paint)
    }

    private fun drawLeaf(canvas: Canvas,x:Int,y:Int){
        canvas.drawBitmap(mLeafBitmap,(x + (mItemWidth - mLeafWidth) / 2).toFloat(),(y + (mItemHeight - mLeafHeight) / 2).toFloat(),null)
    }

    override fun onDrawSelected(
        canvas: Canvas,
        calendar: Calendar,
        x: Int,
        y: Int,
        hasScheme: Boolean
    ): Boolean {
        //drawRoundRect(canvas,x,y,mSelectBgRadius,mSelectBgPaint)
        drawLeaf(canvas,x,y)
        return false
    }

    override fun onDrawScheme(canvas: Canvas, calendar: Calendar, x: Int, y: Int) {

    }


    /***
     * x,y is left top coordinate
     */
    override fun onDrawText(
        canvas: Canvas,
        calendar: Calendar,
        x: Int,
        y: Int,
        hasScheme: Boolean,
        isSelected: Boolean
    ) {
        canvas.save()

        //draw current day
       /* if(calendar.isCurrentDay && !isSelected){
            drawRoundRect(canvas,x,y,mTodayBgRadius,mCurrentDayBgPaint)
        }*/

        val cx = x + mItemWidth / 2
        //val top = y - mItemHeight / 6

        setTextColor(calendar,isSelected)

        //draw day
        val dayText = calendar.day.toString()
        val dayTextWidth = FontUtil.measureText(dayText,mTextPaint)
        canvas.drawText(dayText, (cx-dayTextWidth/2), mTextBaseLine + y,mTextPaint)

        canvas.restore()

    }

    private fun setTextColor(calendar: Calendar, isSelected: Boolean){
        /*if(calendar.isCurrentDay){

        }else {

        }*/
        if(isSelected){
            mTextPaint.color = mSelectTextColor
        }else{
            if(calendar.isCurrentDay){
                mTextPaint.color = mCurrentDayTextColor
            }else{
                mTextPaint.color = mTextColor
            }
        }
    }

}
